<template>
  <!-- 充电日志 -->
  <div class="box-shadow">
    <!-- 搜索 -->
    <div class="search-area">
      <div class="form">
        <el-form
          ref="queryForm"
          :rules="rulesFrom"
          :model="listQuery"
          size="small"
          :inline="true"
          label-width="100px"
        >
          <el-form-item
            prop="startTime"
            :label="$t('触发时段') + ':'"
            label-width="99px"
          >
            <el-date-picker
              v-model="listQuery.triggerTime"
              style="width: 237px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              :start-placeholder="$t('开始日期')"
              :end-placeholder="$t('结束日期')"
            ></el-date-picker>
          </el-form-item>
          <el-form-item
            class="smallWidth"
            :label="$t('触发电量') + ':'"
            prop="ltriggerVal"
            label-width="110px"
          >
            <el-input
              v-model.number="listQuery.ltriggerVal"
              :placeholder="$t('请输入')"
              clearable
              size="medium"
              type="text"
            />
          </el-form-item>
          <span
            style="
              color: #fff;
              margin-right: 10px;
              display: inline-block;
              height: 36px;
              line-height: 36px;
            "
            >—</span
          >
          <el-form-item class="smallWidth" prop="ltriggerVal1">
            <el-input
              v-model.number="listQuery.ltriggerVal1"
              :placeholder="$t('请输入')"
              clearable
              size="medium"
            />
          </el-form-item>
          <el-form-item
            prop="startTime"
            :label="$t('开始时段') + ':'"
            label-width="106PX"
          >
            <el-date-picker
              v-model="listQuery.beginTime"
              style="width: 237px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              :start-placeholder="$t('开始日期')"
              :end-placeholder="$t('结束日期')"
            ></el-date-picker>
          </el-form-item>
          <el-form-item
            class="smallWidth"
            :label="$t('开始电量') + ':'"
            prop="beginPowerval"
          >
            <el-input
              v-model.number="listQuery.beginPowerval"
              :placeholder="$t('请输入')"
              clearable
              size="medium"
            />
          </el-form-item>
          <span
            style="
              color: #fff;
              margin-right: 10px;
              display: inline-block;
              height: 36px;
              line-height: 36px;
            "
            >—</span
          >
          <el-form-item class="smallWidth" prop="beginPowerval1">
            <el-input
              v-model.number="listQuery.beginPowerval1"
              :placeholder="$t('请输入')"
              clearable
              size="medium"
            />
          </el-form-item>
          <el-form-item prop="startTime" :label="$t('结束时段') + ':'">
            <el-date-picker
              v-model="listQuery.endTime"
              style="width: 237px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              :start-placeholder="$t('开始日期')"
              :end-placeholder="$t('结束日期')"
            ></el-date-picker>
          </el-form-item>

          <el-form-item
            class="smallWidth"
            :label="$t('结束电量') + ':'"
            prop="endPowerval"
            label-width="108px"
          >
            <el-input
              v-model.number="listQuery.endPowerval"
              :placeholder="$t('请输入')"
              clearable
              size="medium"
            />
          </el-form-item>
          <span
            style="
              color: #fff;
              margin-right: 10px;
              display: inline-block;
              height: 36px;
              line-height: 36px;
            "
            >—</span
          >
          <el-form-item class="smallWidth" prop="endPowerval1">
            <el-input
              v-model.number="listQuery.endPowerval1"
              :placeholder="$t('请输入')"
              clearable
              size="medium"
            />
          </el-form-item>
          <el-form-item :label="$t('充电人') + ':'">
            <el-input
              v-model.trim="listQuery.charger"
              :placeholder="$t('请输入')"
              clearable
              size="medium"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item
            class="smallWidth"
            :label="$t('充电时长') + ':'"
            prop="chargeTimeval"
            label-width="128px"
          >
            <el-input
              v-model.number="listQuery.chargeTimeval"
              :placeholder="$t('请输入')"
              clearable
              size="medium"
            />
          </el-form-item>
          <span
            style="
              color: #fff;
              margin-right: 10px;
              display: inline-block;
              height: 36px;
              line-height: 36px;
            "
            >—</span
          >
          <el-form-item class="smallWidth" prop="chargeTimeval1">
            <el-input
              v-model.number="listQuery.chargeTimeval1"
              :placeholder="$t('请输入')"
              clearable
              size="medium"
            />
          </el-form-item>
          <el-form-item :label="$t('充电概述') + ':'" label-width="98px">
            <el-input
              v-model.trim="listQuery.operationContent"
              :placeholder="$t('请输入')"
              clearable
              size="medium"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item :label="$t('充电类型') + ':'" label-width="129px">
            <el-checkbox-group v-model="listQuery.chargeType">
              <el-checkbox :label="0">{{ $t("手动") }}</el-checkbox>
              <el-checkbox :label="1">{{ $t("低电量充电") }}</el-checkbox>
              <el-checkbox :label="2">{{ $t("巡检充电") }}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item :label="$t('充电状态') + ':'" label-width="130px">
            <el-checkbox-group v-model="listQuery.operationType">
              <el-checkbox label="0" name="type">{{ $t("成功") }}</el-checkbox>
              <el-checkbox label="1" name="type">{{ $t("失败") }}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-form>
      </div>
      <div class="search-area-btn btns">
        <div @click="serachBtn" class="serch-btn cuver-point">
          {{ $t("搜索") }}
        </div>
        <div @click="resetFormBtn('listQuery')" class="reset-btn cuver-point">
          {{ $t("重置") }}
        </div>
      </div>
    </div>
    <!-- 表格 -->
    <div class="table">
      <div class="self">
        <div class="search-area-btn btns">
          <div class="serch-btn cuver-point table-top" @click="handleDerive">
            {{ $t("导出") }}
          </div>
          <!-- <div class="serch-btn cuver-point table-top" @click="handleDerive">
            {{ $t("充电测试") }}
          </div> -->
        </div>
        <el-table
          ref="tableRef"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%; margin-top: 16px"
          @selection-change="selectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column align="center" prop="id" :label="$t('日志ID')">
          </el-table-column>
          <el-table-column
            width="100"
            align="center"
            prop="triggerTime"
            :label="$t('触发时间')"
          >
          </el-table-column>
          <el-table-column
            :resizable="false"
            align="center"
            prop="triggerPower"
            :label="$t('触发电量')"
            :formatter="batteryformatter"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="chargeType"
            :label="$t('充电类型')"
          >
            <template slot-scope="scope">
              <div>
                <span>{{
                  scope.row.chargeType == 0
                    ? $t("手动充电")
                    : scope.row.chargeType == 1
                    ? $t("低电量充电")
                    : $t("巡检充电")
                }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="charger" :label="$t('充电人')">
          </el-table-column>
          <el-table-column
            width="100"
            align="center"
            prop="beginTime"
            :label="$t('开始时间')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="beginPower"
            :label="$t('开始电量')"
            :formatter="batteryformatters"
            :resizable="false"
          >
            <template slot-scope="scope">
              <div>
                <span>{{
                  scope.row.beginPower == null ? "" : scope.row.beginPower + "%"
                }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            width="100"
            align="center"
            prop="endTime"
            :label="$t('结束时间')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="endPower"
            :label="$t('结束电量')"
            :formatter="batteryEndPower"
            :resizable="false"
          >
            <template slot-scope="scope">
              <div>
                <span>{{
                  scope.row.endPower == null ? "" : scope.row.endPower + "%"
                }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="chargeTime"
            :label="$t('充电时长') + '(min)'"
            width="170px"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="operationType"
            :label="$t('充电状态')"
          >
            <template slot-scope="scope">
              <div
                :style="{
                  color:
                    scope.row.operationType == 0
                      ? '#73F6AF'
                      : scope.row.operationType == 1
                      ? '#FF5562 '
                      : '',
                }"
              >
                {{
                  scope.row.operationType == 0
                    ? $t("成功")
                    : scope.row.operationType == 1
                    ? $t("失败")
                    : ""
                }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            width="260"
            align="center"
            prop="operationContent"
            :label="$t('充电概述')"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column align="center" :label="$t('操作')">
            <template slot-scope="scope">
              <div class="table-btns">
                <div
                  class="serch-btn cuver-point"
                  @click="handleParticulars(scope.row)"
                >
                  {{ $t("详情") }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagenation">
        <pagination
          :total="total"
          :page.sync="listQuerySubmit.pageNum"
          :limit.sync="listQuerySubmit.pageSize"
          @pagination="getTableList"
        >
        </pagination>
      </div>
    </div>
  </div>
</template>
<script>
import lineChart from "./components/lineChart.vue";
import { API_getCharge } from "@/api/log/cherglog";
export default {
  data() {
    // 表单验证
    var validatePass = (rule, value, callback) => {
      if (!this.listQuery.ltriggerVal && this.listQuery.ltriggerVal1) {
        return callback(new Error(this.$t("请输入完整")));
      }
      callback();
    };
    // 表单验证
    var validatePass1 = (rule, value, callback) => {
      if (!this.listQuery.ltriggerVal1 && this.listQuery.ltriggerVal) {
        return callback(new Error(this.$t("请输入完整")));
      }
      callback();
    };
    // 表单验证
    var beginPowerval = (rule, value, callback) => {
      if (!this.listQuery.beginPowerval && this.listQuery.beginPowerval1) {
        return callback(new Error(this.$t("请输入完整")));
      }
      callback();
    };
    // 表单验证
    var beginPowerval1 = (rule, value, callback) => {
      if (!this.listQuery.beginPowerval1 && this.listQuery.beginPowerval) {
        return callback(new Error(this.$t("请输入完整")));
      }
      callback();
    };
    // 表单验证
    var endPowerval = (rule, value, callback) => {
      if (!this.listQuery.endPowerval && this.listQuery.endPowerval1) {
        return callback(new Error(this.$t("请输入完整")));
      }
      callback();
    };
    // 表单验证
    var endPowerval1 = (rule, value, callback) => {
      if (!this.listQuery.endPowerval1 && this.listQuery.endPowerval) {
        return callback(new Error(this.$t("请输入完整")));
      }
      callback();
    };
    // 表单验证
    var chargeTimeval = (rule, value, callback) => {
      if (!this.listQuery.chargeTimeval && this.listQuery.chargeTimeval1) {
        return callback(new Error(this.$t("请输入完整")));
      }
      callback();
    };
    // 表单验证
    var chargeTimeval1 = (rule, value, callback) => {
      if (!this.listQuery.chargeTimeval1 && this.listQuery.chargeTimeval) {
        return callback(new Error(this.$t("请输入完整")));
      }
      callback();
    };
    return {
      isCollapse: false, //展开折叠
      // 查询条件
      listQuery: {
        triggerTime: [], //触发时间
        triggerPower: "", //触发电量
        beginTime: [], //开始时段
        beginPower: "", //开始电量
        endTime: [], //结束时段
        endPower: "", //结束电量
        charger: "", //充电人
        chargeTime: "", //充电时长
        operationContent: "", //操作内容
        chargeType: [], //充电类型
        operationType: [], //操作状态
        ltriggerVal1: "",
        ltriggerVal: "",
        beginPowerval: "",
        beginPowerval1: "",
        endPowerval: "",
        endPowerval1: "",
        chargeTimeval: "",
        chargeTimeval1: "",
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      listQuerySubmit: {},
      tableData: [], //表格数据
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 折线图数据
      lineChartAll: [
        {
          name: this.$t("CPU使用"),
          type: "line",
          // stack: 'Total',
          data: [20, 23, 25, 21, 29, 33, 21, 38, 20, 23, 25, 21],
        },
      ],
      electricityTime: "",
      walktempTime: "",
      speedTime: "",
      checked: false,
      datas: [],
      // 表单验证
      rulesFrom: {
        ltriggerVal: [{ validator: validatePass, trigger: "blur" }],
        ltriggerVal1: [{ validator: validatePass1, trigger: "blur" }],
        beginPowerval: [{ validator: beginPowerval, trigger: "blur" }],
        beginPowerval1: [{ validator: beginPowerval1, trigger: "blur" }],
        endPowerval: [{ validator: endPowerval, trigger: "blur" }],
        endPowerval1: [{ validator: endPowerval1, trigger: "blur" }],
        chargeTimeval: [{ validator: chargeTimeval, trigger: "blur" }],
        chargeTimeval1: [{ validator: chargeTimeval1, trigger: "blur" }],
      },
    };
  },

  components: {
    lineChart,
  },
  created() {
    this.listQuerySubmit = Object.assign({}, this.listQuery);
    this.getTableList();
  },
  mounted() {},
  methods: {
    // 表格添加 %
    batteryformatter(row) {
      {
        return row.triggerPower + "%";
      }
    },
    batteryformatters(row) {
      {
        return row.beginPower + "%";
      }
    },
    batteryEndPower(row) {
      {
        return row.endPower + "%";
      }
    },

    /** 重置按钮操作 */
    resetFormBtn() {
      this.listQuery = {
        triggerTime: [], //触发时间
        triggerPower: "", //触发电量
        beginTime: [], //开始时段
        beginPower: "", //开始电量
        endTime: [], //结束时段
        endPower: "", //结束电量
        charger: "", //充电人
        chargeTime: "", //充电时长
        operationContent: "", //操作内容
        chargeType: [], //充电类型
        operationType: [], //操作状态
        ltriggerVal1: "",
        ltriggerVal: "",
        beginPowerval: "",
        beginPowerval1: "",
        endPowerval: "",
        endPowerval1: "",
        chargeTimeval: "",
        chargeTimeval1: "",
        pageNum: 1,
        pageSize: 10,
      };
      this.serachBtn();
    },

    /**搜索 */
    serachBtn() {
      this.$refs.queryForm.validate((valid) => {
        if (valid) {
          this.listQuery.pageNum = 1;
          this.listQuerySubmit = Object.assign({}, this.listQuery);
          this.getTableList();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    /** table多选 */
    selectionChange(selection) {
      this.ids = selection.map((item) => item.resourceId);
      this.single = selection.length !== 1;
    },
    /**表格数据 */
    getTableList() {
      const obj = {
        ...this.listQuerySubmit,
        triggerTime: this.timeInfo(this.listQuerySubmit.triggerTime), //触发时间
        triggerPower: this.listQuerySubmit.ltriggerVal
          ? this.listQuerySubmit.ltriggerVal +
            "," +
            this.listQuerySubmit.ltriggerVal1
          : "", //触发电量
        beginTime: this.timeInfo(this.listQuerySubmit.beginTime), //开始时段
        beginPower: this.listQuerySubmit.beginPowerval
          ? this.listQuerySubmit.beginPowerval +
            "," +
            this.listQuerySubmit.beginPowerval1
          : "", //开始电量
        endTime: this.timeInfo(this.listQuerySubmit.endTime), //结束时段
        endPower: this.listQuerySubmit.endPowerval
          ? this.listQuerySubmit.endPowerval +
            "," +
            this.listQuerySubmit.endPowerval1
          : "", //结束电量
        charger: this.listQuerySubmit.charger, //充电人
        chargeTime: this.listQuerySubmit.chargeTimeval
          ? this.listQuerySubmit.chargeTimeval +
            "," +
            this.listQuerySubmit.chargeTimeval1
          : "", //充电时长 chargeTimeval1
        operationContent: this.listQuerySubmit.operationContent, //操作内容
        chargeType: this.listQuerySubmit.chargeType.length
          ? this.listQuerySubmit.chargeType.join(",")
          : "", //充电类型
        operationType: this.listQuerySubmit.operationType.join(","), //操作状态
      };
      API_getCharge(obj).then((res) => {
        console.log(res);
        if (res.code == 200) {
          if (res.rows.length > 0) {
            this.tableData = res.rows;
          } else {
            this.tableData = [];
          }
        }
        this.total = res.total * 1;
        // console.log(this.total);
      });
    },
    timeInfo(val) {
      if (val && val.length) {
        return `${val[0]},${val[1]}`;
      } else {
        return "";
      }
    },

    fn(val) {
      if (val) {
        return `${val},${val}`;
      } else {
        return "";
      }
    },
    // 导出
    handleDerive() {
      this.download(
        "/robot.log/charge.log.export",
        {
          ...this.listQuery,
        },
        `chargelog_${new Date().getTime()}.xlsx`
      );
    },

    // 详情按钮
    handleParticulars(row) {
      console.log(row);
      this.$router.push({
        path: "/details",
        query: {
          id: row.id,
        },
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.table {
  width: 100%;
  height: 770px;
  // margin-top: 16px;
  border-top: 1px dashed rgba(219, 255, 251, 0.2);
  padding: 24px;
  box-sizing: border-box;
  position: relative;
  .btns {
    display: flex;
    width: 100%;
    justify-content: flex-start;
    div:nth-child(2) {
      margin: 0 16px;
    }
  }
  .table-btns {
    display: flex;
    div {
      display: flex;
      // width: 46px;
      height: 28px;
      justify-content: center;
      align-items: center;
      font-size: 12px;
    }
    div:nth-child(2),
    div:nth-child(4) {
      margin: 0 10px;
    }
    div:nth-child(5) {
      margin-right: 10px;
    }
  }
  .pagenation {
    position: absolute;
    bottom: 30px;
    right: 20px;
  }
}
::v-deep {
  .smallWidth {
    .el-form-item__content {
      width: 146px;
    }
  }
  input {
    background-color: rgba(255, 255, 255, 0.15) !important;
    color: #fff !important;
    border: none !important;
  }
  .self {
    .el-table__body-wrapper {
      width: 100%;
      max-height: 531px;
      overflow: auto;
    }
  }
}
.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 146px;
}
::v-deep .el-range-input {
  background: none !important;
}
</style>
